local yingyu = fk.CreateSkill {

  name = "joy__yingyu",

  tags = {  },

}



yingyu:addEffect(fk.EventPhaseStart, {
  name = "joy__yingyu",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(yingyu.name) and
      (player.phase == Player.Play or (player.phase == Player.Finish and player:usedSkillTimes("joy__yongbi", Player.HistoryGame) > 0))
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function(p)
      return not p:isKongcheng() end), Util.IdMapper)
    if #targets < 2 then return end
    local tos = room:askForChoosePlayers(player, targets, 2, 2, "#yingyu-choose", yingyu.name, true)
    if #tos == 2 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target1 = room:getPlayerById(event:getCostData(self)[1])
    local target2 = room:getPlayerById(event:getCostData(self)[2])
    room:doIndicate(player.id, {event:getCostData(self)[1]})
    local id1 = room:askForCardChosen(player, target1, "h", yingyu.name)
    room:doIndicate(player.id, {event:getCostData(self)[2]})
    local id2 = room:askForCardChosen(player, target2, "h", yingyu.name)
    target1:showCards(id1)
    target2:showCards(id2)
    if Fk:getCardById(id1).suit ~= Fk:getCardById(id2).suit and
      Fk:getCardById(id1).suit ~= Card.NoSuit and Fk:getCardById(id1).suit ~= Card.NoSuit then
      local to = room:askForChoosePlayers(player, event:getCostData(self), 1, 1, "#yingyu2-choose", yingyu.name, false)
      if #to > 0 then
        to = to[1]
      else
        to = table.random(event:getCostData(self))
      end
      if to == target1.id then
        room:obtainCard(event:getCostData(self)[1], id2, true, fk.ReasonPrey)
      else
        room:obtainCard(event:getCostData(self)[2], id1, true, fk.ReasonPrey)
      end
    end
  end,
})

return yingyu